/**
 * 
 */
package org.extdev.libraries.ltr;

/**
 * @author LAP
 *
 */
public class LTRAPIFacadeFactory {
	private LTRAPIFacadeFactory() {
		
	}
	
	private static class LTRAPIFacadeFacrotyKeeper {
		private static final LTRAPIFacadeFactory INSTANCE = new LTRAPIFacadeFactory();
	}
	
	public static LTRAPIFacadeFactory getInstance() {
		return LTRAPIFacadeFacrotyKeeper.INSTANCE; 
	}
	
	public LTRAPILibraryFacade getLTRServerFacade() throws LTRException {
		LTRAPILibraryFacade facade = new LTRAPILibraryFacade();
		facade.initilizeLTR();
		facade.setChannel(LTRAPIConstants.CC_CONTROL);
		facade.setCrateSN(LTRAPIConstants.CSN_SERVER_CONTROL.getBytes());
		return facade;
	}
	
	public LTRAPILibraryFacade getLTRCrateFacade(String serialNumber) throws LTRException {
		return getLTRModuleFacade(serialNumber, LTRAPIConstants.CC_CONTROL);
	}
	
	public LTRAPILibraryFacade getLTRModuleFacade(String serialNumber, short moduleNo) throws LTRException {
		modulesCheck(moduleNo);
		LTRAPILibraryFacade facade = new LTRAPILibraryFacade();
		facade.initilizeLTR();
		facade.setChannel(moduleNo);
		facade.setCrateSN(serialNumber.getBytes());
		return facade;
	}
	
	public LTR11APILibraryFacade getLTR11ModuleFacade(String serialNumber, short moduleNo) throws LTRException {
		modulesCheck(moduleNo);
		LTR11APILibraryFacade facade = new LTR11APILibraryFacade();
		facade.inicilizeLTR11();
		facade.setChannel(moduleNo);
		facade.setCrateSN(serialNumber.getBytes());
		return facade;
	}
	
	public LTR212APILibraryFacade getLTR212ModuleFacade(String serialNumber, short moduleNo) throws LTRException {
		modulesCheck(moduleNo);
		LTR212APILibraryFacade facade = new LTR212APILibraryFacade();
		facade.inicilizeLTR212();
		facade.setChannel(moduleNo);
		facade.setCrateSN(serialNumber.getBytes());
		return facade;
	}
	
	private void modulesCheck(short moduleNo) {
		if (moduleNo < 0 || moduleNo > LTRAPIConstants.MODULE_MAX) {
			throw new IllegalArgumentException("Module number can't be " + moduleNo + ", number must be in range of [0..16]");
		}
	}
}
